﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class paginas_Servicos : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            var service = ServiceGerenciador.Model.Service.FindAll();            

            if (service.Length > 0)
                mvCrud.ActiveViewIndex = 0;//controle se existe registro e mostra a view
            else
                mvCrud.ActiveViewIndex = 1;

            gvCrud.DataSource = service;
            gvCrud.DataBind();
        }
    }
   
    //protected void btn_ppesquisar_Click(object sender, EventArgs e)
    //{
    //    //try
    //    //{
    //    //    System.Text.StringBuilder oQuery = new System.Text.StringBuilder();
           
    //    //    oQuery.Append("SELECT  idServico, servico, preco, tempo FROM servicos WHERE upper(servico) like '%" + txt_pesquisar.Text.ToUpper() + "%'");
    //    //    SqlDataSourceServico.SelectCommand = oQuery.ToString();
    //    //    SqlDataSourceServico.DataBind();

    //    //}
    //    //catch (Exception ex)
    //    //{//verificar a exceção
    //    //    Console.WriteLine(ex.Message);

    //    //} 
    //}
    //protected void gvServicos_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    //gvServicos.DataBind();
    //}
    protected void btnPesquisar_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtTermo.Text))
            return;

        var teste = ServiceGerenciador.Model.Service.Pesquisa(txtTermo.Text).FirstOrDefault();

        if (teste == null)
        {
            lblPesquisa.Text = "Serviço inexistente!";            
        }
        else
        {
            lblPesquisa.Text = "";
            gvCrud.DataSource = ServiceGerenciador.Model.Service.Pesquisa(txtTermo.Text);
        }        
        gvCrud.DataBind();
    }
    protected void btnNovo_Click(object sender, EventArgs e)
    {
        Response.Redirect("ServicoNovo.aspx");
    }
    protected void btnLimpar_Click(object sender, EventArgs e)
    {
        Response.Redirect("Servicos.aspx");
    }
}
